﻿package extremefx.modules.css {

	/**
	 * @author Marcelo Volmaro
	 */
	public class ParseCommons {
		public static function parseQuotedText(pString:String):String {
			return pString.replace("\"", "").replace("'", "");
		}
		
		public static function parseDirProp(pString:String):DirProp{
			var ret:Array = pString.split("-");
			var dir:String = ret[1];
			var prop:String;
			
			if (dir == "top" || dir == "right" || dir == "bottom" || dir == "left"){
				prop = ret[2];
				
			} else {
				dir = prop = null;
			}
			
			return new DirProp(dir, prop, dir ? ret.join("-") : null);
		}
		
		public static function parseFour(pString:String):Vector.<String> {
			var args:Array = pString.split(" ");
			var ret:Vector.<String> = new Vector.<String>();
			
			switch (args.length){
				case 1:
					ret.push(args[0]);
					ret.push(args[0]);
					ret.push(args[0]);
					ret.push(args[0]);
					break;
					
				case 2:
					ret.push(args[0]);
					ret.push(args[1]);
					ret.push(args[0]);
					ret.push(args[1]);
					break;
					
				case 3:
					ret.push(args[0]);
					ret.push(args[1]);
					ret.push(args[2]);
					ret.push(args[1]);
					break;
					
				case 4:
					return Vector.<String>(args);
			}
			
			return ret;
		}
		
		public static function pt2px(pPoints:Number):uint {
			return Math.round(pPoints / 0.75);
		}
		
		public static function pc2px(pPicas:Number):uint  {
			return Math.round((pPicas * 12) / 0.75);
		}
		
		public static function in2px(pInches:Number):uint  {
			return Math.round((pInches * 72) / 0.75);
		}
		
		public static function cm2px(pCentimeters:Number):uint  {
			return Math.round((pCentimeters * 28.346) / 0.75);
		}
		
		public static function mm2px(pMillimeters:Number):uint  {
			return Math.round((pMillimeters * 2.835) / 0.75);
		}
		
		public static function isNumber(pIn:String):Boolean {
			return !isNaN(Number(pIn));
		}
		
		public static function parseColor(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			if (pIn.indexOf("#") == 0 && (pIn.length == 7 || pIn.length == 4)) return pIn;
			if (pIn == "transparent") return "#00000000";
			if (pIn.indexOf("rgb(") == 0) {
				var parts:Array = pIn.replace("rgb(", "").replace(")", "").split(",");
				if (parts.length != 3) return null;
				
				var ret:String = "#";
				
				for (var i:uint = 0; i<3; i++){
					var val:Number = parseFloat(parts[i]);
					if (isNaN(val)) return null;
					if (parts[i].indexOf("%") != -1) val = 255 * val / 100;
					ret += val.toString(16); 
				}
				
				return parseColor(ret);
			}
			
			if (pIn == "maroon") return "#800000";
			if (pIn == "red") return "#ff0000";
			if (pIn == "orange") return "#ffA500";
			if (pIn == "yellow") return "#ffff00";
			if (pIn == "olive") return "#808000";
			if (pIn == "purple") return "#800080";
			if (pIn == "fuchsia") return "#ff00ff";
			if (pIn == "white") return "#ffffff";
			if (pIn == "lime") return "#00ff00";
			if (pIn == "green") return "#008000";
			if (pIn == "navy") return "#000080";
			if (pIn == "blue") return "#0000ff";
			if (pIn == "aqua") return "#00ffff";
			if (pIn == "teal") return "#008080";
			if (pIn == "black") return "#000000";
			if (pIn == "silver") return "#c0c0c0";
			if (pIn == "gray") return "#808080";
			
			return null;
		}
		
		public static function parseUrl(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			if (pIn.indexOf("url(") == -1) return null;
			return parseQuotedText(pIn.substr(4, pIn.length-6));
		}
		
		public static function parseRepeat(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			switch (pIn){
				case "no-repeat":
				case "repeat-x":
				case "repeat-y":
				case "repeat":
					return pIn;
			}
			
			return null;
		}
		
		public static function parseAttachment(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			switch (pIn){
				case "scroll":
				case "fixed":
					return pIn;
			}
			
			return null;
		}
		
		public static function parseDirection(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			switch (pIn){
				case "top":
				case "bottom":
				case "center":
				case "left":
				case "right":
					return pIn;
			}
			
			if (isPercent(pIn) || parseLength(pIn)) return pIn;
			return null;
		}
		
		public static function isPercent(pIn:String):Boolean {
			return (pIn.indexOf("%") != -1);
		}
		
		/**
		 * @return null if the value is not a length
		 */
		public static function parseLength(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			if (pIn == "0") return "0";
			
			var unit:String = pIn.substr(pIn.length-2, 2);
			var value:Number = parseFloat(pIn.substr(0, pIn.length-2)) || 0;
			
			switch (unit){
				case "em":
				case "ex":
				case "px":
					//em & ex not supported, return value in px.
					return value.toString();
					
				case "in":
					return in2px(value).toString();
					
				case "cm":
					return cm2px(value).toString();
					
				case "mm":
					return mm2px(value).toString();
					
				case "pt":
					return pt2px(value).toString();
					
				case "pc":
					return pc2px(value).toString();
			}
			
			return null;
		}
		
		public static function parseBorderWidth(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			switch (pIn){
				case "thin":
					return "1";
					
				case "medium":
					return "2";
					
				case "thick":
					return "3";
			}
			
			return parseLength(pIn);
		}
		
		/**
		 * @return an empty string if the property is not a border style
		 */
		public static function parseBorderStyle(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			switch (pIn){
				case "none":
				case "hidden":
				case "dotted":
				case "dashed":
				case "solid":
				case "double":
				case "groove":
				case "ridge":
				case "inset":
				case "outset":
					return pIn;
			}
			
			return null;
		}
		
		public static function parseFontSize(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			switch(pIn){
				case "xx-small":
					return "6";
					
				case "x-small":
				case "smaller":
					return "8";
					
				case "small":
					return "10";
					
				case "medium":
					return "12";
					
				case "large":
					return "14";
					
				case "x-large":
				case "larger":
					return "16";
					
				case "xx-large":
					return "18";
			}
			
			if (isPercent(pIn)) {
				return Math.round(parseFloat(pIn.replace("%","")) * 12 / 100).toString();
			}
			
			return parseLength(pIn);
		}
		
		public static function parseFontStyle(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			switch (pIn){
				case "normal":
				case "oblique":
				case "italic":
					return pIn;
			}
			
			return null;
		}
		
		public static function parseFontVariant(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			switch (pIn){
				case "normal":
				case "small-caps":
					return pIn;
			}
			
			return null;
		}
		
		public static function parseFontWeight(pIn:String):String {
//			pIn = StringTools.trim(pIn);
			switch (pIn){
				case "normal":
					return "400";
					
				case "bold":
					return "700";
					
				case "bolder":
					return "700";
					
				case "lighter":
					return "100";
					
				case "100":
				case "200":
				case "300":
				case "400":
				case "500":
				case "600":
				case "700":
				case "800":
				case "900":
					return pIn;
			}
			
			return null;
		}
	}
}
